20230322
[shlib.git] / testing / 1.env_testing.Testing for environment.unit / 1.shell_interpreter_fix_testing.sh
blob31214e188c87d1849692bb66b5c5cc4ded15362d
1 #!/bin/bash
2 ############################################################
3 # source: shell_interpreter_fix_testing.sh
4 # author: devenkong(18151155@qq.com)
5 # date: 2021-10-02
6 ############################################################
7 # Copyright (C) 2022- Free Software Foundation, Inc.
8 # This configure script is free software; the Free Software
9 # Foundation gives unlimited permission to copy, distribute
10 # and modify it.
11 ############################################################
12 # note:
13 # 测试脚本解释器是修正程序fix-shebang。
14 ############################################################
17 TESTING_ROOT_DIR=
18 TESTING_TMP_DIR=~/.testing/$0/
21 main ()
23 local name=
24 local path=
26 echo "\n1.无参数。对文件中原有的interpreter进行路径修正,如果不存在,输出提示信息。"
27 rm "$TESTING_TMP_DIR/" -rf
28 mkdir -p "$TESTING_TMP_DIR"
29 cp 1.* *.sh scripts "$TESTING_TMP_DIR" -rf
30 cd "$TESTING_TMP_DIR"
31 chmod +x *.sh scripts/*
33 echo -ne "[shebang]:" > a.txt
34 head -n 1 scripts/1.interpreter_sh_with_wrong_path.sh > a.txt
36 which bash
37 which sh
38 fix-shebang scripts/1.interpreter_sh_with_wrong_path.sh
40 echo -ne "[shebang]:" > b.txt
41 head -n 1 scripts/1.interpreter_sh_with_wrong_path.sh > b.txt
43 if [[ -n `diff a.txt b.txt` ]]; then
44 echo "interpreter fixed."
45 else
46 echo "interpreter not fixed."
48 ./scripts/1.interpreter_sh_with_wrong_path.sh
49 [[ $? != 0 ]] && echo "interpreter not fixed"
51 cd - > /dev/null
53 exit
55 echo "\n2.-c参数。脚本自带的interpreter无效时,使用默认interpreter。"
56 rm "$TESTING_TMP_DIR/" -rf
57 mkdir -p "$TESTING_TMP_DIR"
58 cp 1.* *.sh scripts "$TESTING_TMP_DIR" -rf
59 cd "$TESTING_TMP_DIR"
60 chmod +x *.sh scripts/*
62 echo -ne "[shebang]:" > a.txt
63 head -n 1 scripts/1.interpreter_abc_not_valid_in_system.sh > a.txt
65 fix-shebang -c scripts/*
67 echo -ne "[shebang]:" > b.txt
68 head -n 1 scripts/1.interpreter_abc_not_valid_in_system.sh > b.txt
70 if [[ -n `diff a.txt b.txt` ]]; then
71 echo "interpreter fixed."
72 else
73 echo "interpreter not fixed."
75 ./scripts/1.interpreter_abc_not_valid_in_system.sh
76 [[ $? != 0 ]] && echo "interpreter not fixed"
78 cd - > /dev/null
81 echo "\n3.-i参数。不对文件进行处理,只输出信息。"
82 rm "$TESTING_TMP_DIR/" -rf
83 mkdir -p "$TESTING_TMP_DIR"
84 cp 1.* *.sh scripts "$TESTING_TMP_DIR" -rf
85 cd "$TESTING_TMP_DIR"
86 chmod +x *.sh scripts/*
88 echo -ne "[shebang]:" > a.txt
89 head -n 1 scripts/{1.interpreter_sh_with_wrong_path.sh, 1.interpreter_abc_not_valid_in_system.sh} > a.txt
91 fix-shebang -i scripts/*
93 echo -ne "[shebang]:" > b.txt
94 head -n 1 scripts/{1.interpreter_sh_with_wrong_path.sh, 1.interpreter_abc_not_valid_in_system.sh} > b.txt
96 if [[ -n `diff a.txt b.txt` ]]; then
97 echo "interpreter fixed."
98 else
99 echo "interpreter not fixed."
101 ./scripts/1.interpreter_sh_with_wrong_path.sh > /dev/null
102 [[ $? != 0 ]] && echo "interpreter not fixed"
103 ./scripts/1.interpreter_abc_not_valid_in_system.sh > /dev/null
104 [[ $? != 0 ]] && echo "interpreter not fixed"
106 cd - > /dev/null
109 echo "\n4.-i -r参数。读取脚本文件1.interpreter_sh_with_wrong_path.sh中的解释器路径和名称,并输出。"
110 rm "$TESTING_TMP_DIR/" -rf
111 mkdir -p "$TESTING_TMP_DIR"
112 cp 1.* *.sh scripts "$TESTING_TMP_DIR" -rf
113 cd "$TESTING_TMP_DIR"
114 chmod +x *.sh scripts/*
116 echo -ne "[shebang]:" > a.txt
117 head -n 1 scripts/{1.interpreter_sh_with_wrong_path.sh, 1.interpreter_bash_with_wrong_path.sh} > a.txt
119 fix-shebang -i -r scripts/*
121 echo -ne "[shebang]:" > b.txt
122 head -n 1 scripts/{1.interpreter_sh_with_wrong_path.sh, 1.interpreter_bash_with_wrong_path.sh} > b.txt
124 if [[ -n `diff a.txt b.txt` ]]; then
125 echo "interpreter fixed."
126 else
127 echo "interpreter not fixed."
130 cd - > /dev/null
133 echo "\n5.-i -v参数。只输出待处理文件的文件名。"
134 rm "$TESTING_TMP_DIR/" -rf
135 mkdir -p "$TESTING_TMP_DIR"
136 cp 1.* *.sh scripts "$TESTING_TMP_DIR" -rf
137 cd "$TESTING_TMP_DIR"
138 chmod +x *.sh scripts/*
140 echo -ne "[shebang]:" > a.txt
141 head -n 1 scripts/{1.interpreter_sh_with_wrong_path.sh, 1.interpreter_bash_with_wrong_path.sh} > a.txt
143 fix-shebang -i -v scripts/*
145 echo -ne "[shebang]:" > b.txt
146 head -n 1 scripts/{1.interpreter_sh_with_wrong_path.sh, 1.interpreter_bash_with_wrong_path.sh} > b.txt
148 if [[ -n `diff a.txt b.txt` ]]; then
149 echo "interpreter fixed."
150 else
151 echo "interpreter not fixed."
154 cd - > /dev/null
157 echo "\n6.-t参数。只对指定扩展名进行处理。"
158 rm "$TESTING_TMP_DIR/" -rf
159 mkdir -p "$TESTING_TMP_DIR"
160 cp 1.* *.sh scripts "$TESTING_TMP_DIR" -rf
161 cd "$TESTING_TMP_DIR"
162 chmod +x *.sh scripts/*
164 echo -ne "[shebang]:" > a.txt
165 head -n 1 scripts/* > a.txt
167 fix-shebang -t .bzsh -c scripts/*
169 echo -ne "[shebang]:" > b.txt
170 head -n 1 scripts/* > b.txt
172 if [[ -n `diff a.txt b.txt` ]]; then
173 echo "interpreter fixed."
174 else
175 echo "interpreter not fixed."
177 ./scripts/1.interpreter_bzsh_with_extname_bzsh_for_filter.sh
178 [[ $? != 0 ]] && echo "interpreter not fixed"
180 cd - > /dev/null
183 echo "\n7.-d参数。对指定interpreter进行处理。"
184 rm "$TESTING_TMP_DIR/" -rf
185 mkdir -p "$TESTING_TMP_DIR"
186 cp 1.* *.sh scripts "$TESTING_TMP_DIR" -rf
187 cd "$TESTING_TMP_DIR"
188 chmod +x *.sh scripts/*
190 echo -ne "[shebang]:" > a.txt
191 head -n 1 scripts/1.interpreter_xyz_used_for_replace_testing.sh > a.txt
193 fix-shebang -d xyz scripts/1.interpreter_xyz_used_for_replace_testing.sh
195 echo -ne "[shebang]:" > b.txt
196 head -n 1 scripts/1.interpreter_xyz_used_for_replace_testing.sh > b.txt
198 if [[ -n `diff a.txt b.txt` ]]; then
199 echo "interpreter fixed."
200 else
201 echo "interpreter not fixed."
203 ./scripts/1.interpreter_xyz_used_for_replace_testing.sh
204 [[ $? != 0 ]] && echo "interpreter not fixed"
206 cd - > /dev/null
209 echo "\n8.-s参数。设定一个默认interpreter。"
210 rm "$TESTING_TMP_DIR/" -rf
211 mkdir -p "$TESTING_TMP_DIR"
212 cp 1.* *.sh scripts "$TESTING_TMP_DIR" -rf
213 cd "$TESTING_TMP_DIR"
214 chmod +x *.sh scripts/*
216 echo -ne "[shebang]:" > a.txt
217 head -n 1 scripts/{1.interpreter_sh_with_wrong_path.sh, 1.interpreter_bash_with_wrong_path.sh} > a.txt
219 fix-shebang -d bash -s sh scripts/{1.interpreter_sh_with_wrong_path.sh, 1.interpreter_bash_with_wrong_path.sh}
221 echo -ne "[shebang]:" > b.txt
222 head -n 1 scripts/{1.interpreter_sh_with_wrong_path.sh, 1.interpreter_bash_with_wrong_path.sh} > b.txt
224 if [[ -n `diff a.txt b.txt` ]]; then
225 echo "interpreter fixed."
226 else
227 echo "interpreter not fixed."
229 ./scripts/1.interpreter_sh_with_wrong_path.sh > /dev/null
230 [[ $? != 0 ]] && echo "interpreter not fixed"
231 ./scripts/1.interpreter_bash_with_wrong_path.sh
232 [[ $? != 0 ]] && echo "interpreter not fixed"
234 cd - > /dev/null
237 echo "\n9.-f参数。对所有待处理文件强制使用默认或-s设定的interpreter。"
238 rm "$TESTING_TMP_DIR/" -rf
239 mkdir -p "$TESTING_TMP_DIR"
240 cp 1.* *.sh scripts "$TESTING_TMP_DIR" -rf
241 cd "$TESTING_TMP_DIR"
242 chmod +x *.sh scripts/*
244 echo -ne "[shebang]:" > a.txt
245 head -n 1 scripts/1.interpreter_sh_with_wrong_path.sh > a.txt
247 fix-shebang -f scripts/*
249 echo -ne "[shebang]:" > b.txt
250 head -n 1 scripts/1.interpreter_sh_with_wrong_path.sh > b.txt
252 if [[ -n `diff a.txt b.txt` ]]; then
253 echo "interpreter fixed."
254 else
255 echo "interpreter not fixed."
257 ./scripts/1.interpreter_sh_with_wrong_path.sh > /dev/null
258 [[ $? != 0 ]] && echo "1.interpreter_sh_with_wrong_path.sh interpreter not fixed"
259 ./scripts/1.interpreter_bash_with_wrong_path.sh > /dev/null
260 [[ $? != 0 ]] && echo "1.interpreter_sh_with_wrong_path.sh interpreter not fixed"
261 ./scripts/1.interpreter_xyz_used_for_replace_testing.sh
262 [[ $? != 0 ]] && echo "1.interpreter_sh_with_wrong_path.sh interpreter not fixed"
264 cd - > /dev/null
267 echo "\n10.解释器带参数。"
268 rm "$TESTING_TMP_DIR/" -rf
269 mkdir -p "$TESTING_TMP_DIR"
270 cp 1.* *.sh scripts "$TESTING_TMP_DIR" -rf
271 cd "$TESTING_TMP_DIR"
272 chmod +x *.sh scripts/*
274 echo -ne "[shebang]:" > a.txt
275 head -n 1 scripts/1.interpreter_sh_with_parameter.sh > a.txt
277 fix-shebang -f scripts/*
279 echo -ne "[shebang]:" > b.txt
280 head -n 1 scripts/1.interpreter_sh_with_parameter.sh > b.txt
282 if [[ -n `diff a.txt b.txt` ]]; then
283 echo "interpreter fixed."
284 else
285 echo "interpreter not fixed."
287 ./scripts/1.interpreter_sh_with_parameter.sh > /dev/null
288 [[ $? != 0 ]] && echo "1.interpreter_sh_with_parameter.sh interpreter not fixed"
290 cd - > /dev/null
292 echo "\n11.解释器带参数。"
293 rm "$TESTING_TMP_DIR/" -rf
294 mkdir -p "$TESTING_TMP_DIR"
295 cp 1.* *.sh scripts "$TESTING_TMP_DIR" -rf
296 cd "$TESTING_TMP_DIR"
297 chmod +x *.sh scripts/*
299 echo -ne "[shebang]:" > a.txt
300 head -n 1 scripts/1.interpreter_sh_with_parameter.sh > a.txt
302 fix-shebang -f scripts/*
304 echo -ne "[shebang]:" > b.txt
305 head -n 1 scripts/1.interpreter_sh_with_parameter.sh > b.txt
307 if [[ -n `diff a.txt b.txt` ]]; then
308 echo "interpreter fixed."
309 else
310 echo "interpreter not fixed."
312 ./scripts/1.interpreter_sh_with_parameter.sh > /dev/null
313 [[ $? != 0 ]] && echo "1.interpreter_sh_with_parameter.sh interpreter not fixed"
315 cd - > /dev/null
318 echo "\n12.参数组合。find * | xargs fix-shebang -r -t .sh -d sh -s bash -v -f"
319 rm "$TESTING_TMP_DIR"/ -rf
320 mkdir -p "$TESTING_TMP_DIR"
321 cp 1.* *.sh scripts "$TESTING_TMP_DIR" -rf
322 cd "$TESTING_TMP_DIR"
323 chmod +x *.sh scripts/*
325 echo -ne "[shebang]:" > a.txt
326 head -n 1 scripts/{1.interpreter_sh_with_wrong_path.sh, 1.interpreter_bash_with_wrong_path.sh} > a.txt
328 fix-shebang -r -t .sh -d sh -s bash -v -f scripts/*
330 echo -ne "[shebang]:" > b.txt
331 head -n 1 scripts/{1.interpreter_sh_with_wrong_path.sh, 1.interpreter_bash_with_wrong_path.sh} > b.txt
333 if [[ -n `diff a.txt b.txt` ]]; then
334 echo "interpreter fixed."
335 else
336 echo "interpreter not fixed."
338 ./scripts/1.interpreter_sh_with_wrong_path.sh
339 ./scripts/1.interpreter_bash_with_wrong_path.sh
341 cd - > /dev/null
346 main